package com.cpoopc.fuckapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

import com.cpoopc.fuckapp.bean.Account;
import com.cpoopc.fuckapp.dao.DatabaseHelper;
import com.cpoopc.fuckapp.utils.DebugLog;
import com.cpoopc.fuckapp.utils.GlobalToast;
import com.cpoopc.fuckapp.utils.security.MD5;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @Bind(R.id.account)
    AutoCompleteTextView mAccountTv;

    @Bind(R.id.password)
    EditText mPwdEdt;

    @Bind(R.id.sign_in_button)
    Button mLoginRegisterBtn;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mAccountTv.setText("hehee");
        databaseHelper = DatabaseHelper.getInstance(this);
    }

    @OnClick(R.id.sign_in_button)
    protected void onSign() {
        String accoutName = getAccoutName();
        String passWord = getPassWord();
        if (!checkAvalidate(accoutName, passWord)) {
            return;
        }
        String pwdMd5 = MD5.getMD5(passWord);
        DebugLog.e(accoutName);
        DebugLog.e(passWord);
        DebugLog.e(pwdMd5);
        try {
            Account account = databaseHelper.getAccountDao().queryBuilder().where().eq("account_name", accoutName).queryForFirst();
            if (account != null) {
                // 账号存在,验证密码
                if (TextUtils.equals(account.getPasswordMD5(), pwdMd5)) {
                    DebugLog.e("登陆成功!");
                    GlobalToast.showToast(this, "登陆成功!");
                } else {
                    DebugLog.e("密码错误 密码:" + account.getPassword() + " md5密码:" + account.getPasswordMD5());
                    GlobalToast.showToast(this, "密码错误 密码:" + account.getPassword() + " md5密码:" + account.getPasswordMD5());
                }
            } else {
                // 账号不存在,创建账号
                Account newAccount = new Account();
                newAccount.setAccountName(accoutName);
                newAccount.setPassword(passWord);
                newAccount.setPasswordMD5(pwdMd5);
                int i = databaseHelper.getAccountDao().create(newAccount);
                DebugLog.e("创建成功? " + i);
                GlobalToast.showToast(this, "创建成功? " + i);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private boolean checkAvalidate(String accoutName, String passWord) {
        if (TextUtils.isEmpty(accoutName) || TextUtils.isEmpty(passWord)) {
            GlobalToast.showToast(this, "账号或者密码不能为空");
            return false;
        }
        return true;
    }

    private String getAccoutName() {
        return mAccountTv.getText().toString();
    }

    private String getPassWord() {
        return mPwdEdt.getText().toString();
    }

}
